/**
 * Created by Administrator on 2016/12/8.
 */
function listDept(parameter){
    var actual={
        el:"",
        input:"",
        inputId:"",
        inputClass:"",
        dataList:[],
        padding:"",
        width:"100%;",
        margin:"0;",
        method:function(){

        }
    }
    var arr=[];
    var brr=[];
    /**
     * 合并对象  清除事件
     */
    function removeEvent(){
        for(var x in parameter){
            if(actual[x]!=undefined){
                actual[x]=parameter[x];
            }
        }
        actual.el.unbind();
        actual.input.unbind();
        $("#selectFistUl").unbind();
        if($("#selectFistUl").attr("id")!=undefined){
            $("#selectFistUl").remove();
        }
    }
    removeEvent();

    /**
     * 渲染 列表参数
     */
    function render(val){
        setTimeout(function () {
            arr=[];
            brr=[];
            actual.dataList.forEach(function (x) {
                if((x.name.indexOf(val)!==(-1)&&val!=="")||val==1){
                    var deptName="";
                    if(x.pId!==undefined){
                        actual.dataList.forEach(function (y) {
                            if(x.pId==y.id){
                                deptName=y.name;
                            }
                        });
                    }
                    if(x.deptName!==undefined){
                        deptName=x.deptName;
                    }
                    brr.push(x);
                    arr.push('<li class="selectFistUlLi" style="list-style: none;padding: 5px 0;border-bottom:1px solid #dedede;cursor:pointer;;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;position: relative;" class="tooltip-test" data-toggle="tooltip" title="'+x.deptNameRank+'">'+x.deptNameRank+'</li>');
                }
            });
            if($("#selectFistUl").attr("id")===undefined&&arr.length>0){
                actual.el.append('<ul id="selectFistUl" style="width:'+actual.width+';position: absolute;left: 0;background-color: #FFFFFF;border: 1px solid #dedede;max-height:200px;z-index:2000;overflow-y:scroll;'+actual.padding+'margin:'+actual.margin+'">'+arr.join("")+'</ul>');
            }else{
                $("#selectFistUl").html(arr.join(""));
            }
        },500);
    }
    render();

    actual.el.on("click",".selectFistUlLi",function () {
        var index=$(this).index();
        $("#selectFistUl").remove();
        actual.method(actual.input,brr[index],brr);
    });
    if(actual.inputId!==""){
        actual.el.on('input propertychange',"#"+actual.inputId, function() {
            var _this=$(this);
            var val=_this.val();
            render(val);
        });
        actual.el.on('click',"#"+actual.inputId, function() {
            var _this=$(this);
            var val=_this.val();
            if(val===""){
                render(1);
            }
        });
    }
    if(actual.inputClass!==""){
        actual.el.on('input propertychange',"."+actual.inputClass, function() {
            var _this=$(this);
            var val=_this.val();
            render(val);
        });
        actual.el.on('click',"."+actual.inputClass, function() {
            var _this=$(this);
            var val=_this.val();
            if(val===""){
                render(1);
            }
        });
    }
    var isinput=false;
    actual.el.mouseover(function () {
        //console.log(1);
        isinput=false;
    });
    actual.el.mouseout(function () {
        isinput=true;
    });
    actual.input.blur(function () {
        if(isinput){
            $("#selectFistUl").remove();
        }
    });
}